package com.inspectortime.webapp.inspector;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;

import com.inspectortime.domain.Inspector;
import com.inspectortime.util.SpringValidatorUtils;

/**
 * Add a new Inspector to an existing company
 * 
 * @author bones
 *
 */
public class EditInspectorController extends EditEmployeeController {
	
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		Inspector inspector = repository.loadInspector(id);
		inspector.setEmailConfirm(inspector.getEmail());
		
		// Trick to not validate for uniqueness if no change
		inspector.setOldEmail(inspector.getEmail());
		inspector.setOldUserName(inspector.getUserName());
		
		return inspector;
	}
	
	private static Log log = LogFactory.getLog(EditInspectorController.class);
	
	protected void onBindAndValidate(HttpServletRequest request,
			Object command, BindException errors) throws Exception {

		Inspector inspector = (Inspector) command;
		
		String param = request.getParameter("limitDrivingPerDay");
		if (param != null && param.equals("on")) {
			inspector.setLimitDrivingPerDay(true);
		} else {
			inspector.setLimitDrivingPerDay(false);
		}
		SpringValidatorUtils.logValidationErrors(log, errors);
	}
	
}
